package com.sali.config;

import cn.hutool.core.thread.ThreadFactoryBuilder;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import java.util.concurrent.*;

/**
 * @description: 公共线程池
 * @author: sali
 * @date: 2025/2/21 下午10:42.
 * @version: 1.0
 */
@Configuration
public class ThreadPoolManager {

    @Bean(name = "commonExecutor")
    public ExecutorService commonExecutor(ThreadConfig threadConfig) {
        ThreadFactory threadFactory = ThreadFactoryBuilder.create().setNamePrefix("common-thread-pool-").build();
        return new ThreadPoolExecutor(threadConfig.getCorePoolSize(), threadConfig.getMaximumPoolSize(), threadConfig.getKeepAliveTime(), TimeUnit.SECONDS, new ArrayBlockingQueue<>(threadConfig.getCapacity()), threadFactory);
    }

}
